""==============
""===
""===	set
""===
""==============
""设置行号
set number
""设置语法高亮
syntax on
""设置相对行号
set relativenumber
""在光标处显示一条线
""set cursorline
""设置字不会超出屏幕，会换行显示，但是仍然在同一行
set wrap
""显示自己输入了什么命令
set showcmd
""在输入命令时，按tab可以有选项提示
set wildmenu
""设置文件格式为utf-8
set encoding=utf-8
""设置搜索高亮
set hlsearch
""设置搜索输入时，高亮可以同步
set incsearch
""搜索忽略大小写
set ignorecase
""搜索只认大写
set smartcase
""显示末尾空格
set list
""更改末尾空格显示
set listchars=tab:▸\ ,trail:♪
set scrolloff=8
""设置按p可以直接从系统剪切板粘贴
""set clipboard=unnamedcol
"
""==============
""===
""===	let
""===
""==============
"
""将<LEADER>改为空格键
let mapleader=" "
"
"
""==============
""===
""===	map
""===
""==============


map sh :set nosplitright<CR>:vsplit<CR>
map sj :set splitright<CR>:vsplit<CR>
map sk :set nosplitbelow<CR>:split<CR>
map sl :set splitbelow<CR>:split<CR>

map <C-h> <C-w>h
map <C-j> <C-w>l
map <C-l> <C-w>j
map <C-k> <C-w>k

map <LEADER><up> :res +5<CR>
map <LEADER><down> :res -5<CR>
map <LEADER><left> :vertical resize-5<CR>
map <LEADER><right> :vertical resize+5<CR>
"
"
""==============
""===
""===	noremap
""===
""==============
""方向键(移动)
""
""	      ^
""	      k
""
""	<h         j>
""	
""	      l
""	      v
""
noremap j l
noremap l j
noremap H 7h
noremap J 7l
noremap L 7j
noremap K 7k
noremap ff gg=Ggi
noremap tn :tabe<CR>
noremap t, :-tabnext<CR>
noremap t. :+tabnext<CR>
noremap V :call setreg("l",line('.'))<CR>V
noremap v :call setreg("n",col('.'))<CR>v
nnoremap q ZZ
nnoremap Q ZZ
nnoremap z q
noremap fa zf
noremap fb zo
noremap . $
noremap , 0
noremap n nzz
noremap N Nzz
noremap <LEADER><CR> :nohlsearch<CR>
nnoremap R :w<CR>:source $MYVIMRC<CR>
nnoremap U vU
nnoremap <C-S> <ESC>:w<CR>
nnoremap <C-q> <ESC>:q!<CR>
nnoremap <C-z> <ESC>u
nnoremap <C-x> <ESC><c-r>
nnoremap <C-P> <ESC><C-I>
"
""==============
""===
""===	inoremap
""===
""==============
inoremap <c-s> <ESC>:w<CR>i
inoremap <C-q> <ESC>:q!<CR>
inoremap <C-z> <ESC>ui
inoremap <C-x> <ESC><c-r>i
inoremap ff <ESC>gg=Ggi
inoremap <C-P> <ESC><C-i>i
inoremap <C-O> <ESC><C-O>i
"
""==============
""===
""===	exec
""===
""==============
exec "nohlsearch"
"
""==============
""===
""===	auto
""===
""==============
"
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
""==============
""===
""===	plug
""===
""==============
call plug#begin('~/.config/nvim/plugged')

"状态栏美化插件
Plug 'https://github.wisteria.cf/vim-airline/vim-airline'
"主题插件
Plug 'https://github.wisteria.cf/connorholyday/vim-snazzy'
"目录树插件
Plug 'https://github.wisteria.cf/scrooloose/nerdtree'
"vim-go插件
Plug 'https://github.wisteria.cf/fatih/vim-go', { 'do': ':GoUpdateBinaries' }
"coc补全插件
Plug 'https://github.wisteria.cf/neoclide/coc.nvim', {'branch': 'release'}
""标签插件
Plug 'https://github.wisteria.cf/kshenoy/vim-signature'
""翻译"
Plug 'https://github.wisteria.cf/voldikss/vim-translator'
"fzf模糊查找
Plug 'https://github.wisteria.cf/junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'https://github.wisteria.cf/junegunn/fzf.vim'
"多光标
Plug 'https://github.wisteria.cf/mg979/vim-visual-multi'

call plug#end()


""==================
""===
""===	配置主题
""===
""==================
if filereadable($HOME.'/.config/nvim/plugged/vim-snazzy/colors/snazzy.vim')
	color snazzy
endif

""==================
""===
""===	配置fzf
""===
""==================
nnoremap <C-f> :Files<CR>




""==================
""===
""===	配置vim-go
""===
""==================
if get(g:, "go_doc_keywordprg_enabled", 1)
	" keywordprg doesn't allow to use vim commands, override it
	nnoremap <buffer> <silent> ¥ :GoDoc<cr>
endif

""==================
""===
""===	配置nerdtree
""===
""==================
nnoremap tt :NERDTreeToggle<CR>
"显示隐藏文件
let NERDTreeShowHidden=1
"不显示以下类型
let NERDTreeIgnore = [ 
			\ '.git$',
			\ '.swp$',
			\ ]
"如果只剩下nerdtree则退出
autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif
"修改箭头
let g:NERDTreeDirArrowExpandable = '+'
let g:NERDTreeDirArrowCollapsible = '-'
"let g:NERDTreeMapOpenInTab = 'o'

""=======================
""===
""===	配置coc自动补全
""===
""=======================
" python3 -m pip install --user --upgrade pynvim
" pip install pylint
" pip install jedi
let g:coc_global_extensions = [
			\ 'coc-actions',
			\ 'coc-go',
			\ 'coc-css',
			\ 'coc-diagnostic',
			\ 'coc-flutter-tools',
			\ 'coc-gitignore',
			\ 'coc-html',
			\ 'coc-json',
			\ 'coc-lists',
			\ 'coc-prettier',
			\ 'coc-snippets',
			\ 'coc-sourcekit',
			\ 'coc-stylelint',
			\ 'coc-syntax',
			\ 'coc-tasks',
			\ 'coc-todolist',
			\ 'coc-translator',
			\ 'coc-tslint-plugin',
			\ 'coc-tsserver',
			\ 'coc-vimlsp',
			\ 'coc-vetur',
			\ 'coc-yaml',
			\ 'coc-yank']
"允许不保存跳转
set hidden
set updatetime=100
set shortmess+=c
"tab补全
inoremap <silent><expr> <TAB>
			\ pumvisible() ? "\<C-n>" :
			\ <SID>check_back_space() ? "\<TAB>" :
			\ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"
function! s:check_back_space() abort
	let col = col('.') - 1
	return !col || getline('.')[col - 1]  =~# '\s'
endfunction

" 使用ctrl空格调出自动补全
inoremap <silent><expr> <c-space> coc#refresh()
"使用CR确定选项
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
			\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
"跳转到上一个错误
nmap <silent> g, <Plug>(coc-diagnostic-prev)
"跳转到下一个错误
nmap <silent> g. <Plug>(coc-diagnostic-next)
"跳转到函数定义处
nmap <silent> gd <Plug>(coc-definition)
"跳转到函数类型定义处
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)

nnoremap <silent> <leader>h :call <SID>show_documentation()<CR>

"===============
"==
"==	一键运行
"==
"===============
func! CompileRunGcc()
	exec "w"
	if &filetype == 'c'
		exec "!g++ % -o %<"
		exec "!time ./%<"
	elseif &filetype == 'cpp'
		set splitbelow
		exec "!g++ -std=c++11 % -Wall -o %<"
		:sp
		:res -15
		:term ./%<
	elseif &filetype == 'java'
		set splitbelow
		:sp
		:res -5
		term javac % && time java %<
	elseif &filetype == 'sh'
		:!time bash %
	elseif &filetype == 'python'
		set splitbelow
		:sp
		:term python3 %
	elseif &filetype == 'html'
		silent! exec "!".g:mkdp_browser." % &"
	elseif &filetype == 'markdown'
		exec "InstantMarkdownPreview"
	elseif &filetype == 'tex'
		silent! exec "VimtexStop"
		silent! exec "VimtexCompile"
	elseif &filetype == 'dart'
		exec "CocCommand flutter.run -d ".g:flutter_default_device." ".g:flutter_run_args
		silent! exec "CocCommand flutter.dev.openDevLog"
	elseif &filetype == 'javascript'
		set splitbelow
		:sp
		:term node %
	elseif &filetype == 'go'
		set splitbelow
		:sp
		:term go run .
	endif
endfunc

map <LEADER>s :call CompileRunGcc()<CR>

"===============
"==
"==	一键注释
"==
"===============
func! Annotation()
	"	echo &filetype
	if &filetype == 'vim'
		call setreg('z',"0i\"")
	elseif &filetype == "go"
		call setreg('z',"0i\/\/")
	elseif &filetype == 'sh'
		call setreg('z',"0i\#")
	elseif &filetype == 'python'
		call setreg('z',"0i\#")
	elseif &filetype == 'css'
		call setreg('z',"\<ESC>0i\/\*\<ESC>A\*\/")
	elseif &filetype == 'html'
		call setreg('z',"\<ESC>0i\<\!\-\-\<ESC>A\-\-\>")
	endif
	:normal @z
endfunc
inoremap bb <ESC>:call Annotation()<CR>a


"===============
"==
"==	配置翻译
"==
"===============
"nnoremap <silent> fy <Plug>TranslateW
nmap <silent> fy <Plug>TranslateW
vmap <silent> fy <Plug>TranslateWV
let g:translator_default_engines = [
			\ 'youdao',
			\ 'haici',
			\ 'bing']


"========================
"==
"==	signature标签插件
"==
"========================
let g:SignatureMap = {
			\ 'Leader'             :  "m",
			\ 'PlaceNextMark'      :  "m,",
			\ 'ToggleMarkAtLine'   :  "m.",
			\ 'PurgeMarksAtLine'   :  "dm-",
			\ 'DeleteMark'         :  "dm",
			\ 'PurgeMarks'         :  "dm/",
			\ 'PurgeMarkers'       :  "dm?",
			\ 'GotoNextLineAlpha'  :  "m<LEADER>",
			\ 'GotoPrevLineAlpha'  :  "",
			\ 'GotoNextSpotAlpha'  :  "m<LEADER>",
			\ 'GotoPrevSpotAlpha'  :  "",
			\ 'GotoNextLineByPos'  :  "",
			\ 'GotoPrevLineByPos'  :  "",
			\ 'GotoNextSpotByPos'  :  "mn",
			\ 'GotoPrevSpotByPos'  :  "mp",
			\ 'GotoNextMarker'     :  "",
			\ 'GotoPrevMarker'     :  "",
			\ 'GotoNextMarkerAny'  :  "",
			\ 'GotoPrevMarkerAny'  :  "",
			\ 'ListLocalMarks'     :  "m/",
			\ 'ListLocalMarkers'   :  "m?"
			\ }



"===============
"==
"==	配置模板
"==
"===============
noremap <LEADER>f :call TemplateFormat()<CR>
function! TemplateFormat()
	if &filetype == "html"
		r !echo -e "<\!DOCTYPE html>\n<html>\n<head>\n	<meta charset="UTF-8">\n	<title></title>\n</head>\n<body>\n\n\n</body>\n</html>"
		normal ggddG
	elseif &filetype == "sh"
		r !echo -e "\#\!/bin/bash"
		normal ggddG
	elseif &filetype == "go"
		r !echo -e "package main\n\nimport "fmt"\n\nfunc main() {\n 	fmt.Println("♧")\n}\n
		normal ggddG
	endif
endfunction

"=======================
"==
"==	一键括号
"==
"=======================
func! AutoBracket()
"	echo &filetype
	if &filetype == 'html'
		call setreg('y',col('.'))
		call setreg('z',"\<ESC>viwda<\<ESC>pa>")
		let autobracket = @y+1
		call setreg('x',"a\<\/\<ESC>pa>\<ESC>")
		call setreg('y',",".autobracket."j")
"		\<\/\<ESC>pa>\<ESC>gi
	:normal @z
	:normal @x
	:normal @y
	endif

endfunc
inoremap HH <ESC>:call AutoBracket()<CR>a



"===============
"==
"==	配置模板
"==
"===============
autocmd BufNewFile *.py 0r ~/.config/nvim/template/python.py
